﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Data.EntityClient;
using DAL.BAL;

namespace WpfClient.Windows
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        const string internalConnection = "Через Сеть";

        const string externalConnection = "Через Интернет";

        public LoginWindow()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region Init connection
                EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();

                if (comboBoxEdit.SelectedIndex == 0)
                    ecsb.Name = "WPFClient_Iternal";
                else
                    ecsb.Name = "WPFClient_External";

                EntityConnection ec = new EntityConnection(ecsb.ConnectionString);

                ec.Open();

                Business.Init(ec);
                #endregion

                Business.Login(textEditUserName.Text, passwordBoxEdit.Text);

                LoadedData.Init();

                this.DialogResult = true;
                this.Close();
            }
            catch(Exception er)
            {
                MessageBox.Show(er.Message,"Ошыбка",MessageBoxButton.OK,MessageBoxImage.Error );
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FocusManager.SetFocusedElement(this, textEditUserName);

            comboBoxEdit.Items.Add(internalConnection);

            comboBoxEdit.Items.Add(externalConnection);
            
            comboBoxEdit.SelectedIndex = 0;
        }
    }
}
